package controllers

import (
	"WebSvr/modules/common"
	"WebSvr/modules/mysql"
	"net/http"

	"text/template"

	webs2cmsg "WebSvr/ProtoMessage/S2CMsg"

	"github.com/astaxie/beego"
	"github.com/golang/protobuf/proto"
)

type AnnounceController struct {
	beego.Controller
}

func (this *AnnounceController) GetAnnounceTab() {
	if !common.GStopFlag {
		ip := this.Ctx.Input.IP()
		webAdmin := mysql.GetWebAdmin()
		userName, accessInt := webAdmin.GetAccessByIp(ip)
		switch int32(accessInt) {
		case common.ACCESS_ADMIN:
			fallthrough
		case common.ACCESS_SERVER:
			{
				this.Data["xsrf_token"] = this.XSRFToken()
				this.Data["adminName"] = userName
				webAdmin := mysql.GetWebAdmin()
				announceText := webAdmin.GetAnnounce()
				this.Data["AnnounceText"] = announceText
				RecordUserAction(userName, accessInt, ip, "GetAnnounceTab成功")
				this.TplName = "announce.html"
			}
		case common.ACCESS_REPORT:
			fallthrough
		default:
			{
				RecordUserAction(userName, accessInt, ip, "GetAnnounceTab失败,无此用户")
				this.CustomAbort(http.StatusUnauthorized, "GetAnnounceTab失败,无此用户")
			}
		}
	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}
}

func (this *AnnounceController) escapeC2SString() common.AnnounceText {
	announceC2S := this.Input().Get("announce")
	sendtimeC2S := this.Input().Get("sendtime")

	/*转换预防XSS*/
	announceEscape := template.JSEscapeString(announceC2S)
	sendtimeEscape := template.JSEscapeString(sendtimeC2S)

	var announceText common.AnnounceText
	announceText.Announce = announceEscape
	announceText.SendTime = sendtimeEscape
	return announceText
}

func (this *AnnounceController) PostSendAnnounceText() {
	if !common.GStopFlag {
		ip := this.Ctx.Input.IP()
		webAdmin := mysql.GetWebAdmin()
		userName, accessInt := webAdmin.GetAccessByIp(ip)
		switch int32(accessInt) {
		case common.ACCESS_ADMIN:
			fallthrough
		case common.ACCESS_SERVER:
			{
				dataEscape := this.escapeC2SString()
				bsucc := webAdmin.UpdateAnnounce(dataEscape)
				if bsucc {
					RecordUserAction(userName, accessInt, ip, "发送公告成功")
					this.CustomAbort(http.StatusOK, "send succ")
				} else {
					RecordUserAction(userName, accessInt, ip, "发送公告失败,mysql错误")
					this.CustomAbort(http.StatusServiceUnavailable, "发送公告失败")
				}
			}
		case common.ACCESS_REPORT:
			fallthrough
		default:
			{
				RecordUserAction(userName, accessInt, ip, "发送公告失败,权限不足")
				this.CustomAbort(http.StatusUnauthorized, "发送公告失败,权限不足")
			}
		}
	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}

}

func (this *AnnounceController) GetAnnounceContent() {
	if !common.GStopFlag {
		webAdmin := mysql.GetWebAdmin()
		announceText := webAdmin.GetAnnounce()
		//组装protobuf
		strData, err := this.marshalAnnounceText(announceText)
		if err == nil {
			this.CustomAbort(http.StatusOK, strData)
		} else {
			this.CustomAbort(http.StatusServiceUnavailable, "get announce text err")
		}
	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}

}

func (this *AnnounceController) marshalAnnounceText(str string) (strData string, err error) {
	msgS2CAnnounce := &webs2cmsg.ServerAnnounceS2C{}
	msgS2CAnnounce.AnnounceStr = proto.String(str)

	// 进行编码
	dataS2C, err := proto.Marshal(msgS2CAnnounce)
	if err != nil {
		return "", err
	}

	strData = string(dataS2C)

	return strData, nil
}

func (this *AnnounceController) Finish() {
	requestbody := this.Ctx.Request.Body
	if requestbody != nil {
		defer requestbody.Close()
	}
}
